<?php

namespace App\Sidong\Controllers;

use App\Admin\Extensions\AuditGroupCourseOrder;
use App\Models\GroupCourse;
use App\Models\GroupCourseOrder;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;
use Encore\Admin\Widgets\Tab;

class GroupCourseOrderController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '团课报名';

    public function index(Content $content)
    {
        $tab = new Tab();

//        $tab->add('全部', $this->grid()->render());
        $tab->add('待核销', $this->grid(GroupCourseOrder::STATUS_PENDING)->render());
        $tab->add('已核销', $this->grid(GroupCourseOrder::STATUS_USED)->render());
        $tab->add('已取消', $this->grid(GroupCourseOrder::STATUS_CANCELED)->render());

        return $content
            ->title($this->title())
            ->breadcrumb(['text' => '团课报名列表'])
//            ->body($this->grid());
            ->body($tab->render());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid($status = 0)
    {
        $grid = new Grid(new GroupCourseOrder);
        $admin = \Admin::user();
        $grid->model()
            ->where('tenant_id', $admin->tenant_id)
            ->where('is_paid', true)
            ->orderByDesc('course_date')
            ->paginate(10);
        $grid->disableBatchActions();
        $grid->disableExport();
        $grid->disableCreateButton();
        $grid->quickSearch('order_sn', 'transaction_id', 'openid');
        if ($status) {
            $grid->model()->where('status', $status);
        }
//        if ($status && $status == GroupCourseOrder::STATUS_PENDING) {
//            $grid->actions(function ($actions) {
//                // 去掉查看
//                $actions->disableView();
//                $actions->disableEdit();
//                $actions->disableDelete();
//                $actions->append(new AuditGroupCourseOrder($actions->getKey()));
//            });
//        } else {
//            $grid->disableActions();
//        }
        $grid->disableActions();

        $grid->column('id', 'ID');
        $grid->column('user.realname', '报名用户')->display(function($realname) {
            return $realname . "({$this->user->phone})";
        });
        $grid->column('groupCourse.title', '课程名称')->display(function($title) {
            $src = \Storage::disk(config('admin.upload.disk'))->url($this->groupCourse->cover_url);
            return '<img  height="35" style="margin-right: 5px;" src="' . $src . '"/>' . $title;
        });
        $grid->column('course_coach_name', '上课教练')->display(function() {
            $src = \Storage::disk(config('admin.upload.disk'))->url($this->groupCourse->coach->cover_url);
            return '<img  height="35" style="margin-right: 5px;" src="' . $src . '"/>' . $this->groupCourse->coach->realname . "({$this->groupCourse->coach->phone})";
        });
        $grid->column('course_time', '上课时间')->display(function() {
            return $this->course_date . ' ' . GroupCourse::WEEKS[$this->groupCourse->week] . ' ' . $this->course_start_time . '~' . $this->course_end_time;
        });
        $grid->column('total_fee', '支付金额')->display(function($total_fee) {
            return  $total_fee > 0 ? '¥' . $total_fee : '免费';
        });

        if ($status && $status == GroupCourseOrder::STATUS_PENDING) {
            $grid->column('paid_time', '支付时间');
            $grid->column('status', '核销状态(点击核销)')->display(function() {
                return $status_display = '<span class="label label-warning">待核销</span>';;
            })->modal('核销二维码', function ($model) {
                $qrcode_field = config('admin.custom.group_course_order.weapp.audit_qrcode_field');
                $qrcode = $this->$qrcode_field ? asset($this->$qrcode_field) : '';
                $html = <<<HTML
<div class="panel text-center">
    <div class="panel-heading">请教练使用小程序扫描下方二维码核销课程</div>
    <div class="panel-body">
        <img src="$qrcode" alt="核销二维码" width="200px" height="230px">
    </div>
</div>
HTML;

                return $html;
            });
        } else {
            $grid->column('status', '核销状态')->display(function() {
                switch ($this->status) {
                    case GroupCourseOrder::STATUS_USED:
                        $status_display = '<span class="label label-success">已核销</span>';
                        break;
                    case GroupCourseOrder::STATUS_CANCELED:
                        $status_display = '<span class="label label-danger">已取消</span>';
                        break;
                }
                return $status_display;
            });
        }

        if ($status && $status == GroupCourseOrder::STATUS_USED) {
            $grid->column('coach_name', '核销教练')->display(function() {
                return $this->status == GroupCourseOrder::STATUS_USED ? $this->groupCourse->coach->realname . "({$this->groupCourse->coach->phone})" : '';
            });
            $grid->column('audit_time', '核销时间');
        }
        if ($status != 0 && $status == GroupCourseOrder::STATUS_CANCELED) {
            $grid->column('cancel_time', '取消时间');
            $grid->column('cancel_reason', '取消原因');
        }

        $grid->column('paid_time', '报名时间');
        $grid->footer(function ($query) {
            // 查询出已支付状态的订单总金额
            $data = $query->where('is_paid', 1)->sum('total_fee');
            return "<div style='padding: 10px;'>总收入：¥$data</div>";
        });
        return $grid;
    }

}
